package cn.yaxma.xmpp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Yaxma on 2016/11/3.
 * Email：2678503498@qq.com
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username_edt;
    private EditText password_edt;
    private Button login_btn;
    private Button register_btn;

    private String username;
    private String password;

    private XMPPService mXmppService;
    private static MessageCallback mCallback;

    private static Handler mHandler;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mHandler = new Handler() {
            @Override public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d("LoginActivity", (String) msg.obj);
                switch (msg.what) {
                    case Constant.MESSAGE_LOGIN:
                        if ("登录成功".equals(msg.obj)) {
                            Intent intent = new Intent(LoginActivity.this, ChatActivity.class);
                            startActivity(intent);
                        }
                        break;
                    case Constant.MESSAGE_REGISTER:

                        break;
                    case Constant.MESSAGE_RECEIVE:
                        if (mCallback != null)
                            mCallback.receiveMsg((String) msg.obj);
                        break;


                }
            }
        };

        mXmppService = XMPPService.getConnection();
        mXmppService.connect();

        mXmppService.setXMPPListener(new XMPPListener() {
            @Override void connect(String msg) {
                super.connect(msg);
                Message message = mHandler.obtainMessage();
                message.what = Constant.MESSAGE_CONNECT;
                message.obj = msg;
                mHandler.sendMessage(message);
            }

            @Override void login(String msg) {
                super.login(msg);
                Message message = mHandler.obtainMessage();
                message.what = Constant.MESSAGE_LOGIN;
                message.obj = msg;
                mHandler.sendMessage(message);
            }

            @Override void register(String msg) {
                super.register(msg);
                Message message = mHandler.obtainMessage();
                message.what = Constant.MESSAGE_REGISTER;
                message.obj = msg;
                mHandler.sendMessage(message);
            }

            @Override void getMessage(String from, String msg) {
                super.getMessage(from, msg);
                Message message = mHandler.obtainMessage();
                message.what = Constant.MESSAGE_RECEIVE;
                message.obj = msg;
                mHandler.sendMessage(message);
            }
        });

        username_edt = (EditText) findViewById(R.id.username_edt);
        password_edt = (EditText) findViewById(R.id.password_edt);

        login_btn = (Button) findViewById(R.id.login_btn);
        register_btn = (Button) findViewById(R.id.register_btn);

        login_btn.setOnClickListener(this);
        register_btn.setOnClickListener(this);
    }

    @Override protected void onDestroy() {
        super.onDestroy();
        mXmppService.disconnect();
    }

    @Override public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                username = username_edt.getText().toString().trim();
                password = password_edt.getText().toString().trim();
                mXmppService.login(username, password);
                break;
            case R.id.register_btn:
                username = username_edt.getText().toString().trim();
                password = password_edt.getText().toString().trim();
                mXmppService.register(username, password);
                break;
        }
    }

    public static void setMessageCallback(MessageCallback callback) {
        mCallback = callback;
    }

    public interface MessageCallback {
        void receiveMsg(String msg);
    }
}
